package cate.game.role.world.invite;

import cate.common.game.invite.InviteDef;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.world.invite.comm.Invitee;
import cate.game.role.world.invite.msg.InviteTaskUpdateResp;
import cate.game.role.world.invite.msg.InviteeChangedResp;
import cate.game.util.api.WorldApi;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import easy.java.io.http.LocationSearch;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.annotation.Transient;

import java.util.ArrayList;
import java.util.List;

@Slf4j
/** 分享邀请引流数据 */
public class RoleInvite extends RoleDependent {

    @Getter
    @NoteField(value = "邀请我创角的代码", detail = "roleId=xxx&abc=123")
    private String createCode;

    @Transient
    @Getter
    @NoteField(value = "邀请我登录的代码", detail = "roleId=xxx&abc=123")
    private String loginCode;

    @Transient
    @JsonIgnore
    @Getter
    private String createInviterId;
    @Transient
    @JsonIgnore
    @Getter
    public String loginInviterId;

    @NoteField("创角任务")
    public InviteCreateTask createTask;
    @NoteField("登录任务")
    public InviteLoginTask loginTask;
    @NoteField("红包任务")
    public InviteRedPkgTask redPkgTask;
    @NoteField("等级任务")
    public InviteLvUpTask lvUpTask;

    @NoteField("受邀人列表")
    public List<Invitee> inviteeList;

    public void setCreateCode(String code) {
        if(StringUtils.isNotBlank(code)) {
            this.createCode = code;
            parseCreateCode();
        }
    }
    private void parseCreateCode() {
        try {
            LocationSearch ls = new LocationSearch(this.createCode);
            this.createInviterId = ls.get("roleId");
        } catch (Exception e) {
            log.error("", e);
        }
    }

    public void setLoginCode(String code) {
        if(StringUtils.isNotBlank(code)) {
            this.loginCode = code;
            parseLoginCode();
        }
    }
    private void parseLoginCode() {
        try {
            LocationSearch ls = new LocationSearch(this.loginCode);
            this.loginInviterId = ls.get("roleId");
            redPkgTask.addInvitor(loginInviterId);
        } catch (Exception e) {
            log.error("", e);
        }
    }

    @Override
    public void initialize(Role role) {
        super.initialize(role);

        parseCreateCode();

        if(createTask == null) {
            createTask = new InviteCreateTask();
        }
        createTask.init();
        if(loginTask == null) {
            loginTask = new InviteLoginTask();
        }
        loginTask.init();
        if(redPkgTask == null) {
            redPkgTask = new InviteRedPkgTask();
        }
        redPkgTask.init();
        if(lvUpTask == null) {
            lvUpTask = new InviteLvUpTask();
        }
        lvUpTask.init();
    }

    public Invitee getInvitee(String inviteeRoleId) {
        if(this.inviteeList == null) {
            return null;
        }
        return this.inviteeList.stream().filter(a -> StringUtils.equals(a.id, inviteeRoleId)).findAny().orElse(null);
    }

    public void addInvitee(Invitee invitee) {
        if(this.inviteeList == null) {
            this.inviteeList = new ArrayList<>();
        }
        if(this.inviteeList.size() >= InviteDef.INVITEE_MAX) {
            log.info("已超过最大邀请人数, {}", invitee);
        }
        if(null != getInvitee(invitee.id)) {
            // 已经存在过了，就不重复添加了.
            return;
        }
        this.inviteeList.add(invitee);
        noticeInviteeChanged();
    }

    /** 通知客户端自己的受邀者信息变更了 */
    public void noticeInviteeChanged() {
        InviteeChangedResp.notice(role);
    }

    @Override
    public void onCreate() {
        if(StringUtils.isNotBlank(createInviterId)) {
            WorldApi.dispatchInviteCreateEvent(role.getGame(), role, createInviterId);
        }
    }

    @Override
    public void onEnter() {
        if(StringUtils.isNotBlank(loginInviterId)) {
            WorldApi.dispatchInviteLoginEvent(role.getGame(), role, loginInviterId);
        }
    }

    public void onLevelUp() {
        if(StringUtils.isNotBlank(createInviterId)) {
            WorldApi.dispatchInviteLevelUpEvent(role.getGame(), role, createInviterId);
        }
    }

    @Override
    public void onDaySpan(boolean silence) {
        //createTask.reset();创角人物不重置
        loginTask.reset();
        redPkgTask.reset();
        InviteTaskUpdateResp.build(this).sendNow(role);
    }

    public boolean existInvitee(String inviteeUid) {
        if (inviteeList == null) {
            return false;
        }
        return inviteeList.stream().anyMatch(e -> StringUtils.equals(e.id, inviteeUid));
    }
}
